/*Copyright (c) 2012 Nicolás Zalaquett

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions of
the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
package extrae;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Cursor;
import javafx.scene.control.TextBox;
import javafx.scene.Node;
import extrae.selectorSensores.sensorAN;
import javafx.scene.layout.LayoutInfo;


/**
 * @author lojkoro
 */

public class configuracionAnalogos extends CustomNode{
    // <editor-fold defaultstate="collapsed" desc="Funciones y Variables de Set-Up de Menu">
     var mintasa:Integer=bind minTasa(unidadesMuestra);
     var maxtasa:Integer=bind maxTasa(unidadesMuestra);
     var minNumeroDatos:Integer=bind minnumeroDatos(unidadesMuestra);
     var maxNumeroDatos:Integer=bind maxnumeroDatos(unidadesMuestra);
     var minCota:Number=bind MinCota(Sensores[canalGatillo]);
     var maxCota:Number=bind MaxCota(Sensores[canalGatillo]);
     var resolucion:Number=bind Sensores[canalGatillo].resolucion;
     public function minTasa(unid:Integer):Integer{
         if(unid==0){
            return 20;
            }
        else if(unid==1){
                return 1;
                }
        else if(unid==2){
                return 1;
                }
        else if(unid==3){
                return 1;
                }
        else if(unid==4){
                return 1;
                }
        else if(unid==5){
                return 1;
                }
        else return 1;
         }
     public function minnumeroDatos(unid:Integer):Integer{
         if(unid==0){
            return 1;
            }
        else if(unid==1){
                return 1;
                }
        else if(unid==2){
                return 1;
                }
        else if(unid==3){
                return 1;
                }
        else if(unid==4){
                return 1;
                }
        else if(unid==5){
                return 1;
                }
        else return 1;
         }
     public function maxnumeroDatos(unid:Integer):Integer{
         if(unid==0){
            return 150;
            }
        else if(unid==1){
                return 5000;
                }
        else if(unid==2){
                return 5000;
                }
        else if(unid==3){
                return 5000;
                }
        else if(unid==4){
                return 5000;
                }
        else if(unid==5){
                return 5000;
                }
        else return 1;
         }
     public function maxTasa(unid:Integer):Integer{
         if(unid==0){
            return 5000;
            }
        else if(unid==1){
                return 5000;
                }
        else if(unid==2){
                return 600;
                }
        else if(unid==3){
                return 120;
                }
        else if(unid==4){
                return 24;
                }
        else if(unid==5){
                return 1;
                }
        else return 1;
         }
     public function MinCota(sens:sensorAN):Number{
        return sens.corte;
     }
     public function MaxCota(sens:sensorAN):Number{
        return (sens.corte+sens.pendiente*1024);
     }
     public function unidadesPalabra(unid:Integer):String{
        if(unid==0){
            return "[us]";
            }
        else if(unid==1){
                return "[ms]";
                }
        else if(unid==2){
                return "[s]";
                }
        else if(unid==3){
                return "[m]";
                }
        else if(unid==4){
                return "[h]";
                }
        else if(unid==5){
                return "[d]";
                }
        else return "";
            }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Tasa">
    // <editor-fold defaultstate="collapsed" desc="Valor">
    public var sliderValorTasa: javafx.scene.control.Slider = javafx.scene.control.Slider {
        layoutX: 40.0
        layoutY: 45.0
        min: bind mintasa
        max:bind maxtasa
        //snapToTicks:true
        //majorTickUnit:bind ((maxtasa-mintasa)/2+1 as Integer)
        //minorTickCount:1
        /*action: function():Void{valorTasa=(Number.parseFloat(textBoxValorTasa.text)-(Sensores[canalGatillo] as sensorAN).corte)/(Sensores[canalGatillo] as sensorAN).pendiente;}*/
    }
    def cambiaValorTasaSlider= bind sliderValorTasa.value on replace{
        textBoxValorTasa.text="{sliderValorTasa.value as Integer}";
        }
    public var textBoxValorTasa: javafx.scene.control.TextBox = javafx.scene.control.TextBox {
        layoutX: 0.0
        layoutY: 20.0
        layoutInfo: LayoutInfo {hfill: false
                                hpos:javafx.geometry.HPos.RIGHT}
    }
    def cambiaValorTasa = bind textBoxValorTasa.text on replace {
    try{if(Number.parseFloat(textBoxValorTasa.text)<=maxtasa and Number.parseFloat(textBoxValorTasa.text)>=mintasa){
        valorTasa=Number.parseFloat(textBoxValorTasa.text) as Integer;
        sliderValorTasa.value=valorTasa;}
        else {
            textBoxValorTasa.text="{valorTasa}";
            //////INGRESAR UN VALOR APROPIADO
            }}
    catch(Exception){
        textBoxValorTasa.text="{valorTasa}";
        //////INGRESAR UN VALOR APROPIADO
        };
    }
    public var label: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 20.0
        layoutY: 0.0
        text: "Cada cuanto se toma una muestra :"
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Seleccion Unidades">
    public-read def choiceBoxUnidadesTasa: javafx.scene.control.ChoiceBox = javafx.scene.control.ChoiceBox {
        layoutX: 95.0
        layoutY: 20.0
        items: [ "Microsegundo", "Millisegundo", "Segundo", "Minuto", "Hora", "D\u00EDa", ]
    }
    def cambiaUnidadesTasa = bind choiceBoxUnidadesTasa.selectedItem on replace {
    unidadesMuestra=choiceBoxUnidadesTasa.selectedIndex;
    }
    // </editor-fold>
    public var panel: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 0.0
        content: [ sliderValorTasa, textBoxValorTasa, label, choiceBoxUnidadesTasa, ]
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Numero de Muestras">
    public var label2: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 50.0
        layoutY: 0.0
        text: "Numero de Muestras :"
        textAlignment: javafx.scene.text.TextAlignment.CENTER
        hpos: javafx.geometry.HPos.CENTER
    }
    public var textBoxNumeroMuestras: javafx.scene.control.TextBox = javafx.scene.control.TextBox {
        layoutX: 30.0
        layoutY: 20.0
        layoutInfo: LayoutInfo{width:150}
    }
    def cambiaNumeroDatos = bind textBoxNumeroMuestras.text on replace {
    try{if(Number.parseFloat(textBoxNumeroMuestras.text)<=maxNumeroDatos and Number.parseFloat(textBoxNumeroMuestras.text)>=minNumeroDatos){
        numeroMuestras=Number.parseFloat(textBoxNumeroMuestras.text) as Integer;
        sliderNumeroMuestras.value=numeroMuestras;}
        else {
            textBoxNumeroMuestras.text="{numeroMuestras}";
            //////INGRESAR UN VALOR APROPIADO
            }}
    catch(Exception){
        textBoxNumeroMuestras.text="{numeroMuestras}";
        //////INGRESAR UN VALOR APROPIADO
        };
    }
    public var sliderNumeroMuestras: javafx.scene.control.Slider = javafx.scene.control.Slider {
        visible: true
        layoutX: 35.0
        layoutY: 45.0
        min: bind minNumeroDatos
        max: bind maxNumeroDatos
    }
    def cambiaNumeroMuestrasSlider= bind sliderNumeroMuestras.value on replace{
        textBoxNumeroMuestras.text="{sliderNumeroMuestras.value as Integer}";
        }
    public var panel2: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 0.0
        layoutY: 80.0
        content: [ label2, textBoxNumeroMuestras, sliderNumeroMuestras, ]
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Gatillo">
    public var label3: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 60.0
        layoutY: 0.0
        text: "Gatillo :"
    }

    public var label5: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 0.0
        layoutY: 45.0
        text: "Cota :"
    }

    public var label4: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 0.0
        layoutY: 20.0
        text: "Canal :"
    }

    public var radioButtonGatillo3: javafx.scene.control.RadioButton = javafx.scene.control.RadioButton {
        layoutX: 150.0
        layoutY: 20.0
        text: "3"
        selected:bind canalGatillo==2
        onMouseClicked: function(event):Void{canalGatillo=2;}
    }

    public var radioButtonGatillo2: javafx.scene.control.RadioButton = javafx.scene.control.RadioButton {
        layoutX: 100.0
        layoutY: 20.0
        text: "2"
        selected:bind canalGatillo==1
        onMouseClicked: function(event):Void{canalGatillo=1;}
    }

    public var radioButtonGatillo1: javafx.scene.control.RadioButton = javafx.scene.control.RadioButton {
        layoutX: 50.0
        layoutY: 20.0
        text: "1"
        selected:bind canalGatillo==0
        onMouseClicked: function(event):Void{canalGatillo=0;}
    }

    public var labelUnidades: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 165.0
        layoutY: 45.0
        text: bind "{Sensores[canalGatillo].unidades}"
    }

    public var textBoxGatilloCota: javafx.scene.control.TextBox = javafx.scene.control.TextBox {
        layoutX: 50.0
        layoutY: 40.0
        layoutInfo: LayoutInfo{width:110.0}//__layoutInfo_textBoxGatilloCota
        //text: bind "{cotaGatillo*(Sensores[canalGatillo] as sensorAN).pendiente +(Sensores[canalGatillo] as sensorAN).corte}"
    }
    def cambiaCota = bind textBoxGatilloCota.text on replace {
    try{if(Number.parseFloat(textBoxGatilloCota.text)<=maxCota and Number.parseFloat(textBoxGatilloCota.text)>=minCota){
        cotaGatillo=obtenerCotaBit(Number.parseFloat(textBoxGatilloCota.text),Sensores[canalGatillo]);
        textBoxGatilloCota.text="{obtenerCotaNumero(cotaGatillo,Sensores[canalGatillo])}";
        sliderGatilloCota.value=cotaGatillo;
        }

        else {
            textBoxGatilloCota.text="{obtenerCotaNumero(cotaGatillo,Sensores[canalGatillo])}";
            //////INGRESAR UN VALOR APROPIADO
            }}
    catch(Exception){
        textBoxGatilloCota.text="{obtenerCotaNumero(cotaGatillo,Sensores[canalGatillo])}";
        //////INGRESAR UN VALOR APROPIADO
        };
    }

    public var sliderGatilloCota: javafx.scene.control.Slider = javafx.scene.control.Slider {
        visible: true
        layoutX: 55.0
        layoutY: 65.0
        min:0
        max:1024
        layoutInfo: LayoutInfo{width:100.0}// __layoutInfo_sliderGatilloCota
    }

    def cambiacotaSlider= bind sliderGatilloCota.value on replace{
        textBoxGatilloCota.text="{obtenerCotaNumero(sliderGatilloCota.value,Sensores[canalGatillo])}";
        }

    public var radioButtonGatilloSobre: javafx.scene.control.RadioButton = javafx.scene.control.RadioButton {
        layoutX: 60.0
        layoutY: 0.0
        text: "Sobre"
        selected:bind sobreobajo
        onMouseClicked: function(event):Void{sobreobajo=true;}
    }

    public var radioButtonGatilloBajo: javafx.scene.control.RadioButton = javafx.scene.control.RadioButton {
        layoutX: 120.0
        layoutY: 0.0
        text: "Bajo"
        selected:bind not sobreobajo
        onMouseClicked: function(event):Void{sobreobajo=false;}
    }

    public var label6: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 0.0
        layoutY: 0.0
        text: "Direcci\u00F3n:"
    }

    public var panel4: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 0.0
        layoutY: 85.0
        content: [ radioButtonGatilloSobre, radioButtonGatilloBajo, label6, ]
    }

    public var panel3: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 20.0
        layoutY: 170.0
        content: [ label3, radioButtonGatillo1, label5, label4, radioButtonGatillo3, radioButtonGatillo2, labelUnidades, textBoxGatilloCota, sliderGatilloCota, panel4, ]
    }
    public function obtenerCotaBit(val:Number,sen:sensorAN):Integer{
        return (((val-sen.corte)/sen.pendiente) as Integer);
        }
    public function obtenerCotaNumero(val:Integer,sen:sensorAN):Number{
        return (val*sen.pendiente + sen.corte);
        }
    def cambiaCotaCuandoCambiaGatillo=bind canalGatillo on replace {
        textBoxGatilloCota.text="{obtenerCotaNumero(0,Sensores[canalGatillo])}";
        }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Separadores y Botones">
    public var separator2: javafx.scene.control.Separator = javafx.scene.control.Separator {
        layoutX: 0.0
        layoutY: 155.0
        layoutInfo: LayoutInfo{width:210.0}
    }
    public var separator: javafx.scene.control.Separator = javafx.scene.control.Separator {
        layoutX: 0.0
        layoutY: 70.0
        layoutInfo: LayoutInfo{width:210.0}
    }
    public var buttonGuardarConfiguracion: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 36.0
        layoutY: 280.0
        text: "Guardar"
    }
    public var buttonCancelar: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 118.0
        layoutY: 280.0
        text: "Cancelar"
    }
    //</editor-fold>
    //Panel de configuracion completo
    public var panel5: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 0.0
        layoutY: 10.0
        content: [ panel, separator, panel2, separator2, panel3, buttonGuardarConfiguracion, buttonCancelar, ]
    }
    // <editor-fold defaultstate="collapsed" desc="Valores de Salida">
    public var guardar:Boolean=false;
    public var Sensores:sensorAN[];
    public var cotaGatillo:Integer;
    public var canalGatillo:Integer;
    public var sobreobajo:Boolean;
    public var valorTasa:Integer;
    public var unidadesMuestra:Integer;
    public var numeroMuestras:Integer;
    //</editor-fold>
    public var detalles: Group = Group {
        visible: true
        layoutX: 5
        layoutY: 10
        content: [panel5,]
        cursor: Cursor.HAND
    }
    public override function create():Node {
        return Group {
            content: [
                detalles,
            ]
        };
    }
}
